package com.water.smartfarm.login;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.water.smartfarm.R;
import com.water.smartfarm.main.MainActivity;
import com.water.smartfarm.register.RegisterActivity;
import com.water.smartfarm.tools.Const;
import com.water.smartfarm.tools.DBHelper;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener {
    DBHelper dbHelper;
    SQLiteDatabase db;
    private TextView userText;
    private TextView register_btn;
    private EditText userInput;
    private EditText passInput;
    private Button loginBtn;
    String user;
    String pass;

    private void initView() {
        userText = (TextView) findViewById(R.id.user_text);
        userInput = (EditText) findViewById(R.id.user_input);
        passInput = (EditText) findViewById(R.id.pass_input);
        register_btn = (TextView) findViewById(R.id.register_btn);
        register_btn.setOnClickListener(this);
        loginBtn = (Button) findViewById(R.id.login_btn);
        loginBtn.setOnClickListener(this);
        dbHelper = new DBHelper(this, Const.USER);
        db = dbHelper.getWritableDatabase();
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();
    }


    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            case R.id.login_btn:
                user = userInput.getText().toString().trim();
                pass = passInput.getText().toString().trim();

                loginUser(user, pass);
                //startActivity(new Intent(LoginActivity.this,MainActivity.class));
                break;
            case R.id.register_btn:
                startActivity(new Intent(LoginActivity.this, RegisterActivity.class));
                break;
        }
    }

    private void loginUser(String user, String pass) {
        try {
            String sql = "select * from USER where name=? and pass=?";
            Cursor cursor = db.rawQuery(sql, new String[]{user, pass});
            if (cursor.getCount() > 0) {
                Toast.makeText(getApplicationContext(), "登录成功",
                        Toast.LENGTH_SHORT).show();

                startActivity(new Intent(LoginActivity.this, MainActivity.class));
                finish();
            } else {
                Toast.makeText(getApplicationContext(), "登录失败",
                        Toast.LENGTH_SHORT).show();
            }
            cursor.close();
        } catch (SQLiteException e)
        {
            Toast.makeText(getApplicationContext(), "登录失败",
                    Toast.LENGTH_SHORT).show();
        }
    }
}
